package com.guide.utils;

import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	public static <T> T readValueString(String content, Class<T> valueType) {
		try {
			if (StringUtils.isNotEmpty(content)) {
				return objectMapper.readValue(content, valueType);
			} else {
				return null;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static <T> T readValue(InputStream ins, Class<T> valueType) {
		try {
			if (ins != null) {
				return objectMapper.readValue(ins, valueType);
			} else {
				return null;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String writeValueString(Object value) {
		try {
			if (value != null) {
				return objectMapper.writeValueAsString(value);
			} else {
				return null;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
